package com.r2coding.system.service.impl;

import com.r2coding.system.vo.req.WebsiteReqVo;
import com.r2coding.system.vo.resp.WebsiteRespVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.r2coding.common.util.PageUtils;
import com.r2coding.common.util.Query;

import com.r2coding.system.dao.WebsiteDao;
import com.r2coding.system.entity.WebsiteEntity;
import com.r2coding.system.service.WebsiteService;
import org.springframework.util.CollectionUtils;

/**
 * 网站配置信息
 *
 * @author 98k灬
 * @email hk666zx@163.com
 * @date 2022-07-16 22:51:34
 */
@Service("websiteService")
public class WebsiteServiceImpl extends ServiceImpl<WebsiteDao, WebsiteEntity> implements WebsiteService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<WebsiteEntity> page = this.page(
                new Query<WebsiteEntity>().getPage(params),
                new QueryWrapper<WebsiteEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public WebsiteRespVo info() {
        WebsiteRespVo respVo = new WebsiteRespVo();
        WebsiteEntity website = this.baseMapper.selectById(1);
        BeanUtils.copyProperties(website, respVo);
        List<Integer> loginTypes = new ArrayList<>();
        if (website.getIsQq() == 1) {
            loginTypes.add(1);
        }
        if (website.getIsWx() == 1) {
            loginTypes.add(2);
        }
        if (website.getIsWb() == 1) {
            loginTypes.add(3);
        }
        respVo.setLoginTypes(loginTypes);
        return respVo;
    }

    @Override
    public void updateInfo(WebsiteReqVo req) {
        WebsiteEntity website = this.baseMapper.selectById(1);
        BeanUtils.copyProperties(req, website);
        // 设置登录方式
        if (!CollectionUtils.isEmpty(req.getLoginTypes())) {
            if (req.getLoginTypes().contains(1)) {
                website.setIsQq(1);
            }else {
                website.setIsQq(0);
            }

            if (req.getLoginTypes().contains(2)) {
                website.setIsWx(1);
            } else {
                website.setIsWx(0);
            }

            if (req.getLoginTypes().contains(3)) {
                website.setIsWb(1);
            } else {
                website.setIsWb(0);
            }
        } else {
            website.setIsQq(0);
            website.setIsWx(0);
            website.setIsWb(0);
        }
        // 设置展示方式
        if (!CollectionUtils.isEmpty(req.getShowList())) {
            if (req.getShowList().contains(1)) {
                website.setIsQqShow(1);
            } else {
                website.setIsQqShow(0);
            }

            if (req.getShowList().contains(2)) {
                website.setIsPhoneShow(1);
            } else {
                website.setIsPhoneShow(0);
            }

            if (req.getShowList().contains(3)) {
                website.setIsGiteeShow(1);
            } else {
                website.setIsGiteeShow(0);
            }
        } else {
            website.setIsQqShow(0);
            website.setIsPhoneShow(0);
            website.setIsGiteeShow(0);
        }
        website.setUpdateTime(new Date());
        this.baseMapper.updateById(website);
    }
}